package com.jason.www.util;

import com.jason.www.exception.CoreException;
import org.apache.commons.lang.StringUtils;

import java.util.Comparator;
import java.util.List;

public class ExcelParserStrategy {
	private String sheetName;
	private int headerStartRow;
	private int headerRowHeight;
	private int headerStartCol;
	private int headerEndCol;
	private String[] headerNames;
	private Class<?> clazz;
	private CellConfig[] cells;
	private List<MergedRegion> mergedRegions;

	public List<MergedRegion> getMergedRegions() {
		return mergedRegions;
	}

	public void setMergedRegions(List<MergedRegion> mergedRegions) {
		this.mergedRegions = mergedRegions;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public int getHeaderStartRow() {
		return headerStartRow;
	}

	public void setHeaderStartRow(int headerStartRow) {
		this.headerStartRow = headerStartRow - 1;
	}

	public int getHeaderRowHeight() {
		return headerRowHeight;
	}

	public void setHeaderRowHeight(int headerRowHeight) {
		this.headerRowHeight = headerRowHeight;
	}

	public int getHeaderStartCol() {
		return headerStartCol;
	}

	public void setHeaderStartCol(int headerStartCol){
		this.headerStartCol = headerStartCol;
	}
	
	public void setHeaderEndCol(int headerEndCol){
		this.headerEndCol = headerEndCol;
	}
	
	public void setHeaderStartCol(String headerStartCol) {
		if (StringUtils.isEmpty(headerStartCol)) {
			this.headerStartCol = -1;
		}
		this.headerStartCol = parseY(headerStartCol) - 1;
	}

	public int getHeaderEndCol() {
		return headerEndCol;
	}

	public void setHeaderEndCol(String headerEndCol) {
		if (StringUtils.isEmpty(headerEndCol)) {
			this.headerEndCol = -1;
		}
		this.headerEndCol = parseY(headerEndCol) - 1;
	}

	public String[] getHeaderNames() {
		return headerNames;
	}

	public void setHeaderNames(String[] headerNames) {
		this.headerNames = headerNames;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}

	public CellConfig[] getCells() {
		return cells;
	}

	public void setCells(CellConfig[] cells) {
		this.cells = cells;
	}

	private short parseY(String yVal) {
		// 去掉前后空白
		yVal = yVal.trim();
		short result = 0;
		for (int i = 0; i < yVal.length(); i++) {
			char t = yVal.charAt(i);
			short a = (short) t;
			if (a < 65 || a > 90) {
				return 0;
			}
			result += a - 64;
		}
		return result;
	}

	public boolean isAllCellIndexed() {
		for (CellConfig cell : cells) {
			if (cell.getIndex() == -1) {
				return false;
			}
		}
		return true;
	}

	public void validateMergeRegionAndSimple() throws CoreException {
		int tmpRowFrom = 0;
		int tmpRowTo = 0;
		boolean change = false;
		for (int i = 0; i < mergedRegions.size(); i++) {
			MergedRegion cellConfig = mergedRegions.get(i);
			int rowFrom = cellConfig.getRowFrom();
			int rowTo = cellConfig.getRowTo();
			if (rowFrom != tmpRowFrom) {
				tmpRowFrom = rowFrom;
				change = true;
			}
			if (change) {
				tmpRowTo = rowTo;
				change = false;
			}else if(tmpRowTo == rowTo){
				mergedRegions.remove(i--);
			}else{
				throw new CoreException("表格不规整");
			}
		}
	}

	public static class CellConfig {
		// excel属性，合并方式
		private String type;
		// 单元格保存值类型
		private int valType;
		private String valName;
		private boolean isArr;
		private int index = -1;

		public CellConfig() {
		}

		public CellConfig(int valType, String valName, boolean isArr) {
			this.valType = valType;
			this.valName = valName;
			this.isArr = isArr;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public int getValType() {
			return valType;
		}

		/**
		 * 1 对应String, 2 对应Boolean, 3 对应Date, 4 对应Double
		 * 
		 * @param valType
		 */
		public CellConfig setValType(int valType) {
			this.valType = valType;
			return this;
		}

		public String getValName() {
			return valName;
		}

		public CellConfig setValName(String valName) {
			this.valName = valName;
			return this;
		}

		public boolean isArr() {
			return isArr;
		}

		public CellConfig setArr(boolean isArr) {
			this.isArr = isArr;
			return this;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

	}

	public static class MergedRegion {
		private int rowFrom;
		private int rowTo;
		private int colFrom;
		private int colTo;

		public int getRowFrom() {
			return rowFrom;
		}

		public void setRowFrom(int rowFrom) {
			this.rowFrom = rowFrom;
		}

		public int getRowTo() {
			return rowTo;
		}

		public void setRowTo(int rowTo) {
			this.rowTo = rowTo;
		}

		public int getColFrom() {
			return colFrom;
		}

		public void setColFrom(int colFrom) {
			this.colFrom = colFrom;
		}

		public int getColTo() {
			return colTo;
		}

		public void setColTo(int colTo) {
			this.colTo = colTo;
		}

	}

	public static class CellComparator implements Comparator<CellConfig> {

		public int compare(CellConfig o1, CellConfig o2) {
			return o1.getIndex() - o2.getIndex();
		}

	}

	public static class MergedRegionComparator implements Comparator<MergedRegion> {

		public int compare(MergedRegion o1, MergedRegion o2) {
			int tmp = o1.getRowFrom() - o2.getRowFrom();
			if (tmp != 0) {
				return tmp;
			}
			tmp = o1.getColFrom() - o2.getColFrom();
			return tmp;
		}

	}
}
